{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "# oracle 数据库链接与操作\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "## 引用模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cx_Oracle  # 引用模块cx_Oracle\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 链接创建对象\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<cx_Oracle.Cursor on <cx_Oracle.Connection to SQL_LEVEL1@192.168.10.122:1521/oracle>>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "conn = cx_Oracle.connect('SQL_LEVEL1/dbcloud@192.168.10.122:1521/oracle')  # 连接数据库\n",
    "sql = \"\"\"select *  from customer c\"\"\"\n",
    "c = conn.cursor()  # 获取cursor\n",
    "# x = c.execute(sql)  # 使用cursor进行各种操作\n",
    "# for i in x:\n",
    "#     print(i)\n",
    "# x.fetchone()\n",
    "c"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 用pandas读取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>CUST_NO</th>\n",
       "      <th>CUST_NAME</th>\n",
       "      <th>CUST_BIRTHDAY</th>\n",
       "      <th>CUST_SEX</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>C_00091</td>\n",
       "      <td>站三</td>\n",
       "      <td>1999-02-01</td>\n",
       "      <td>男</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>C_00001</td>\n",
       "      <td>高爱敏</td>\n",
       "      <td>1986-01-06</td>\n",
       "      <td>女</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>C_00002</td>\n",
       "      <td>朱光昊</td>\n",
       "      <td>1989-07-23</td>\n",
       "      <td>男</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>C_00003</td>\n",
       "      <td>胡煜羲</td>\n",
       "      <td>1990-05-11</td>\n",
       "      <td>男</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>C_00004</td>\n",
       "      <td>黄奕轩</td>\n",
       "      <td>1997-07-04</td>\n",
       "      <td>男</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   CUST_NO CUST_NAME CUST_BIRTHDAY CUST_SEX\n",
       "0  C_00091        站三    1999-02-01        男\n",
       "1  C_00001       高爱敏    1986-01-06        女\n",
       "2  C_00002       朱光昊    1989-07-23        男\n",
       "3  C_00003       胡煜羲    1990-05-11        男\n",
       "4  C_00004       黄奕轩    1997-07-04        男"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.read_sql(sql=sql,con=conn)\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    CUST_NO CUST_NAME CUST_BIRTHDAY CUST_SEX\n",
      "0   C_00091        站三    1999-02-01        男\n",
      "1   C_00001       高爱敏    1986-01-06        女\n",
      "2   C_00002       朱光昊    1989-07-23        男\n",
      "3   C_00003       胡煜羲    1990-05-11        男\n",
      "4   C_00004       黄奕轩    1997-07-04        男\n",
      "..      ...       ...           ...      ...\n",
      "86  C_00086       郑震雨    1985-12-03        女\n",
      "87  C_00087       高悦跃    1982-02-07        女\n",
      "88  C_00088       朱之文    1998-02-19        男\n",
      "89  C_00089       刘佳乐    1991-10-10        女\n",
      "90  C_00090        徐玮    1988-08-21        女\n",
      "\n",
      "[91 rows x 4 columns]\n"
     ]
    }
   ],
   "source": [
    "print(data)\n",
    "c.close()  # 关闭cursor"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 写入数据库\n",
    "\n",
    "\n",
    "导入模块\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sqlalchemy import create_engine, types"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "连接信息解析\n",
    "\n",
    "\n",
    "'oracle+cx_oracle://USERNAME:PASSPORD@HOST:POST/?service_name=DATABASES'\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "engine = create_engine(\n",
    "    'oracle+cx_oracle://SQL_LEVEL1:dbcloud@192.168.10.122:1521/?service_name=oracle')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "| 参数                | 解析                                                     |\n",
    "| ------------------- | -------------------------------------------------------- |\n",
    "| TEST                | 表名                                                     |\n",
    "| con=engine          | 数据库连接对象                                           |\n",
    "| if_exists='replace' | 如果存在这张表那么给他覆盖掉                             |\n",
    "| index=False         | pandas自动生成的索引不添加                               |\n",
    "| index_label=None    | 当上一个参数index为True时，设置写入数据表时index的列名称 |\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data.to_sql('TEST', con=engine, if_exists='replace',index=False, index_label=None)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
